package com.baizhi.bzmall.admin.service.impl;

import com.baizhi.bzmall.admin.service.RoleService;
import com.baizhi.bzmall.dao.AdminRoleDao;
import com.baizhi.bzmall.dao.RoleDao;
import com.baizhi.bzmall.dao.RoleResourceDao;
import com.baizhi.bzmall.entity.BzAdminRole;
import com.baizhi.bzmall.entity.BzRole;
import com.baizhi.bzmall.entity.BzRoleResource;
import com.baizhi.bzmall.entity.Menu;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private AdminRoleDao ard;
    @Autowired
    private RoleResourceDao rrd;
    @Override
    public List<BzRole> selectAll() {
        return roleDao.selectList(null);
    }

    @Override
    public List<BzAdminRole> selectRoleByAdmin(int adminId) {
        QueryWrapper<BzAdminRole> wrapper=new QueryWrapper<>();
        wrapper.eq("admin_id",adminId);
        return ard.selectList(wrapper);
    }

    @Override
    public List<BzRoleResource> selectResourceByRole(int roleId) {
        QueryWrapper<BzRoleResource> wrapper=new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        return rrd.selectList(wrapper);
    }
    public void grantPermessionToRole(int roleId,int[] perIds){
        QueryWrapper<BzRoleResource> wrapper=new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        rrd.delete(wrapper);
        for (int id:perIds){
            BzRoleResource entity=new BzRoleResource();
            entity.setResourceId(id);
            entity.setRoleId(roleId);
            rrd.insert(entity);
        }
    }
}
